﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Threading;
using AmazonGlacierGUI.Core.Helpers;
using AmazonGlacierGUI.Core.Helpers.AmazonGlacier;
using AmazonGlacierGUI.Core.Infrastructure;
using AmazonGlacierGUI.Core.Model;
using AmazonGlacierGUI.UI.Infrastructure;
using AmazonGlacierGUI.UI.Infrastructure.MVVMFramework;
using AmazonGlacierGUI.UI.Views;
using GalaSoft.MvvmLight.Command;
using AmazonGlacierGUI.Core;
namespace AmazonGlacierGUI.UI.ViewModels
{
    //07-Sep-2012 Priyan R
    public class UploadViewModel:AppViewModelBase
    {
        public UploadViewModel(string[] files)
        {
               
            WireCommands();                  
            var filesVM = new List<FileDetailsVM>();
            foreach (var file in files)
            {
                filesVM.Add(new FileDetailsVM(file));
            }
            Files = filesVM;
        }      
       
        private void OnUploadCommand()
        {
            if (!ValidateAll()) return;
            foreach (var file in Files)
            {
                if (!file.ValidateAll()) return;                
            }
            foreach (var file in Files)
            {
                var job = new JobViewModel();
                job.VaultName = VaultName;
                job.JobType = JobTypeEnum.Upload;
                job.Region = Region.Value;
                job.UploadArchiveFilePath = file.FilePath;
                job.UploadArchiveDescription = file.Description;
                App.JobsVM.AddJob(job);
            }
            Close();
        }
       
       
        private void OnBrowseVaultCommand()
        {
            if (!ValidateProperty(()=>Region)) return;            
            var vm = new VaultBrowserViewModel(Region.Value);       
            UIHelper.ShowVaultBrowser(vm);
            if (vm.SelectedVault != null)
            {
                VaultName = vm.SelectedVault.VaultName;
                Region = vm.Region;
            }

        }
        private void OnCreateNewVaultCommand()
        {

            var vm = new CreateNewVaultViewModel() {Region = Region};           
            UIHelper.ShowCreateNewVault(vm);    
            if(vm.VaultName!=null)
            {
                Region = vm.Region;
                VaultName = vm.VaultName;
            }
       
        }
        void WireCommands()
        {
            UploadCommand=new RelayCommand(OnUploadCommand);            
            BrowseVaultCommand = new RelayCommand(OnBrowseVaultCommand);
            CreateNewVaultCommand = new RelayCommand(OnCreateNewVaultCommand);
        }

        
        public RelayCommand BrowseVaultCommand { get; set; }
        public RelayCommand CreateNewVaultCommand { get; set; }
        public RelayCommand UploadCommand { get; set; }        
        private string _vaultName;
        [Required]
        public string VaultName
        {
            get { return _vaultName; }
            set
            {
                if (_vaultName != value)
                {
                    _vaultName = value;                    
                    RaisePropertyChanged(() => VaultName);
                }
            }
        }
        private AmazonRegionEnum? _region;
        [Required]
        public AmazonRegionEnum? Region
        {
            get { return _region; }
            set
            {
                if (_region != value)
                {
                    _region = value;                    
                    RaisePropertyChanged(() => Region);
                }
            }
        }              

        private List<FileDetailsVM> _files;
        public List<FileDetailsVM> Files
        {
            get { return _files; }
            set
            {
                if (_files != value)
                {
                    _files = value;
                    RaisePropertyChanged(() => Files);
                }
            }
        }
        public class FileDetailsVM : AppViewModelBase
        {
            public FileDetailsVM()
            {
                AddValidation(() => FileName,
                              () =>
                              {
                                  if (!File.Exists(FilePath))
                                  {
                                      return new ValidationResultData("File not exit");
                                  }
                                  return ValidationResultData.Success;
                              });                
            }
            public FileDetailsVM(string filePath)
                : this()
            {
                FilePath = filePath;
                EnableValidtion = true;
            }

            private string _filePath;
            [Required]
            public string FilePath
            {
                get { return _filePath; }
                set
                {
                    if (_filePath != value)
                    {
                        _filePath = value;                      
                        RaisePropertyChanged(() => FilePath);
                        RaisePropertyChanged(() => FileName);
                        RaisePropertyChanged(() => Size);
                        if (Description== null)
                        {
                            Description  = FileName;                            
                        }
                    }
                }
            }
            public string FileName
            {
                get { return Path.GetFileName(FilePath); }
            }           
            private string _description;
            public string Description
            {
                get { return _description; }
                set
                {
                    if (_description != value)
                    {
                        _description = value;                        
                        RaisePropertyChanged(()=>Description);
                    }
                }
            }
            public long Size
            {
                get { return new FileInfo(FilePath).Length; }             
            }
        }
     
    }
}
